<?php

namespace app\enterprise\controller;

use app\enterprise\model\offlinetraining\OfflineTraining as OfflineTrainingModel;
use app\enterprise\model\offlinetraining\OfflineTrainingStaff;
use app\enterprise\model\staff\Staff;
use think\exception\ValidateException;


class OfflineTraining extends Enterprise
{

    public function list()
    {
        $page = input('page', 1);
        $size = input('size', 10);
        $training_name = input('training_name', '');
        $submit_status = input('submit_status', '');
        $start_time = strtotime(input('start_time', ''));
        $end_time = strtotime(input('end_time', ''));

        $data = OfflineTrainingModel::where('status', 1)
            ->where('company_id', $this->adminId)
            ->when($training_name, function ($query) use ($training_name) {
                $query->where('training_name', 'like', '%' . $training_name . '%');
            })
            ->when($submit_status, function ($query) use ($submit_status) {
                $query->where('submit_status', $submit_status);
            })
            ->when($start_time, function ($query) use ($start_time) {
                $query->where('training_time', '>=', $start_time);
            })
            ->when($end_time, function ($query) use ($end_time) {
                $query->where('training_time', '<=', $end_time);
            })
            ->order('id', 'desc')
            ->paginate($size)
            ->each(function ($item) {
                $item->training_time = date('Y-m-d H:i:s', $item->training_time);
                $staff = Staff::getOne($item->staff_id);
                if ($staff) {
                    $item->staff_name = $staff->username;
                } else{
                    $item->staff_name = '';
                }

            });

        return app('json')->success([
            'list' => $data->items(),
            'total' => $data->total(),
            'total_page'=>$data->lastPage(),
        ]);
    }

    public function save()
    {
        $data = $this->request->only([
            'training_name', 'training_time', 'location', 'content', 'staff_id', 'textures',
            'teacher',
        ]);
        try {
            validate([
                'training_name|培训名称' => 'require|max:255',
                'training_time|培训时间' => 'require|dateFormat:Y-m-d H:i:s',
//                'img_url|培训图片' => 'require|max:255',
                'location|培训地点' => 'require|max:255',
                'content|培训内容' => 'require|max:255',
                'staff_id|培训组织人' => 'require|number',
//                'submit_status|提交状态' => 'require|number|in:1,2',
                'teacher|授课老师' => 'require|max:255',
                'textures|备注' => 'max:1024',
            ])->check($data);

            $staffs = input('staffs', []);

            if ($staffs) {
                foreach ($staffs as $staff) {
                    if (!$staff['staff_name']) {
                        return app('json')->fail('请填写培训人员名称');
                    }
                }
            }

            $data['company_id'] = $this->adminId;

            $id = input('id', '');

            $data['training_time'] = strtotime($data['training_time']);
            if (!$id) {
                // 新增
                $res = OfflineTrainingModel::create($data);
                $id = $res->id;
            } else {

                $model = OfflineTrainingModel::getOne($id);
                if (!$model) {
                    return app('json')->fail('数据不存在');
                }
                if ($model->submit_status == 2) {
                    return app('json')->fail('已提交，不能修改');
                }
                if ($model->company_id != $this->adminId) {
                    return app('json')->fail('无权操作此数据');
                }

                OfflineTrainingModel::update($data, ['id' => $id]);
                OfflineTrainingStaff::arrayDelete(['offline_training_id' => $id]);
            }

            if ($staffs) {
                foreach ($staffs as $staff) {
                    OfflineTrainingStaff::create([
                        'offline_training_id' => $id,
                        'staff_id' => $staff['staff_id'],
                        'staff_name'=> $staff['staff_name']
                    ]);
                }
            }
            return app('json')->success('保存成功');
        } catch (ValidateException $exception) {
            return app('json')->fail($exception->getMessage());
        }
    }

    public function info()
    {
        $id = input('id', '');

        $model = OfflineTrainingModel::getOne($id);
        if (!$model) {
            return app('json')->fail('数据不存在');
        }

        if ($model->img_url) {
            $model->img_urls = explode(',', $model->img_url);
        } else{
            $model->img_urls = [];
        }

        $model->trainging_time = date('Y-m-d H:i:s', $model->training_time);
        $staff = Staff::getOne($model->staff_id);
        $model->staff_name = $staff->username;
        $staffs = OfflineTrainingStaff::where('status', 1)->where('offline_training_id', $id)->select();

        return app('json')->success([
            'model' => $model,
            'staffs' => $staffs,
        ]);

    }


    public function delete()
    {
        $id = input('id', '');
        $model = OfflineTrainingModel::getOne($id);
        if (!$model) {
            return app('json')->fail('数据不存在');
        }
        if ($model->company_id != $this->adminId) {
            return app('json')->fail('无权操作此数据');
        }
        OfflineTrainingModel::dataDelete($id);
        OfflineTrainingStaff::arrayDelete(['offline_training_id' => $id]);
        return app('json')->success('删除成功');
    }


}